#!/usr/bin/perl

use POE qw(Component::Client::DAAP);

use Data::Dumper;

POE::Session->create(
    inline_states => {
        _start => \&_start,
        connected => \&connected,
        dbs => \&dbs,
        songs => \&songs,
        test => \&test,
    },
);

sub _start {
    $_[HEAP]->{server} = POE::Component::Client::DAAP->new(
#       debug => 1,
        daap_options => [
            SERVER_HOST => '10.0.0.159',
            SERVER_PORT => '3689',
#           PASSWORD    => $password
        ], # could put something here, or omit it
    );
    warn "started\n";
    $_[KERNEL]->yield('test');
    #$self->connect({ event => 'connected' });
    # alternative way
    $_[HEAP]->{server}->yield(connect => { event => 'connected' });
}

sub connected {
    unless ($_[ARG0]->{result}) {
        warn "connect failed\n";
        $_[HEAP]->{server}->shutdown();
        return;
    }
    warn "connected to $_[ARG0]->{result}\n";
    $_[HEAP]->{server}->databases({ event => 'dbs', extra_data => sub { 'testing!' } });
    warn "getting databases\n";
}

sub dbs {
    warn "databses: ".Data::Dumper->Dump([$_[ARG0]->{result}])." and with extra test data: ".$_[ARG0]->{extra_data}()."\n";
    $_[HEAP]->{server}->songs({ event => 'songs' });
    warn "getting songs\n";
}

sub songs {
#   warn "songs: ".Data::Dumper->Dump([$_[ARG0]->{result}])."\n";
    $_[HEAP]->{server}->shutdown;
    $_[KERNEL]->alarm_remove_all();
}

sub test {
    warn ".\n";
    $_[KERNEL]->delay_set('test' => 1);
}

$poe_kernel->run();

exit;
